package cn.csust.net2.manager.client.module;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.extjs.gxt.ui.client.Style.ButtonScale;
import com.extjs.gxt.ui.client.Style.IconAlign;
import com.extjs.gxt.ui.client.Style.LayoutRegion;
import com.extjs.gxt.ui.client.data.BeanModel;
import com.extjs.gxt.ui.client.event.ButtonEvent;
import com.extjs.gxt.ui.client.event.Events;
import com.extjs.gxt.ui.client.event.Listener;
import com.extjs.gxt.ui.client.event.SelectionListener;
import com.extjs.gxt.ui.client.event.TreePanelEvent;
import com.extjs.gxt.ui.client.util.Margins;
import com.extjs.gxt.ui.client.widget.ContentPanel;
import com.extjs.gxt.ui.client.widget.Info;
import com.extjs.gxt.ui.client.widget.Label;
import com.extjs.gxt.ui.client.widget.LayoutContainer;
import com.extjs.gxt.ui.client.widget.TabItem;
import com.extjs.gxt.ui.client.widget.TabPanel;
import com.extjs.gxt.ui.client.widget.Window;
import com.extjs.gxt.ui.client.widget.button.Button;
import com.extjs.gxt.ui.client.widget.button.ButtonGroup;
import com.extjs.gxt.ui.client.widget.form.SimpleComboBox;
import com.extjs.gxt.ui.client.widget.form.TextField;
import com.extjs.gxt.ui.client.widget.form.ComboBox.TriggerAction;
import com.extjs.gxt.ui.client.widget.layout.BorderLayout;
import com.extjs.gxt.ui.client.widget.layout.BorderLayoutData;
import com.extjs.gxt.ui.client.widget.layout.FitData;
import com.extjs.gxt.ui.client.widget.layout.FitLayout;
import com.extjs.gxt.ui.client.widget.toolbar.SeparatorToolItem;
import com.extjs.gxt.ui.client.widget.toolbar.ToolBar;
import com.google.gwt.core.client.GWT;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.AbstractImagePrototype;

import cn.csust.net2.manager.client.panel.EntranceRegistrationPanel;
import cn.csust.net2.manager.client.panel.StudentInformationPanel;
import cn.csust.net2.manager.client.resources.Resources;
import cn.csust.net2.manager.client.ux.BeanGridPanel;
import cn.csust.net2.manager.client.ux.ExpandGrid;
import cn.csust.net2.manager.client.ux.ExpandTree;
import cn.csust.net2.manager.client.ux.QueryWindow;
import cn.csust.net2.manager.shared.po.PO;
import cn.csust.net2.manager.shared.po.Student;
import cn.csust.net2.manager.shared.service.BaseServiceAsync;
import cn.csust.net2.manager.shared.service.ByGradeService;
import cn.csust.net2.manager.shared.service.ByGradeServiceAsync;
import cn.csust.net2.manager.shared.util.ClassForNameAble;
import cn.csust.net2.manager.shared.vo.Config;
import cn.csust.net2.manager.shared.vo.GridConfig;
import cn.csust.net2.manager.shared.vo.StateProperty;
import cn.csust.net2.manager.shared.vo.TreeNode;

@ClassForNameAble
public class CheckStudentInfo extends BaseModule  {

	// 该界面绑定的服务
	private ByGradeServiceAsync service;
	private ExpandTree treePanel;
	private ExpandGrid<Student> grid;
	private ContentPanel cp;
	private LayoutContainer layout;
	private BorderLayoutData westData;
	private BorderLayoutData centerData;

	public CheckStudentInfo() {

		super();
		initTab();
		initGrid();
		initTree();
		initListeners();
		this.setLayout(new FitLayout());
		this.add(layout);
		this.addStyleName("pad-text");
	}

	private void initTab() {
		/**
		 * 初始化TREE
		 */
		service = GWT.create(ByGradeService.class);
		this.setText("信息审核");
		this.setId("信息审核");
		centerData = new BorderLayoutData(LayoutRegion.CENTER);
		centerData.setMargins(new Margins(0));
		westData = new BorderLayoutData(LayoutRegion.EAST, 200);
		layout = new LayoutContainer();
		layout.setLayoutOnChange(true);
		layout.setLayout(new BorderLayout());
	}

	private void initGrid() {
		grid = new ExpandGrid<Student>(new Student(), service);
		grid.setColumnLines(true);
		cp = new ContentPanel();
		initTool();
		cp.setWidth("100%");
		cp.setHeight("80%");
		cp.setHeaderVisible(false);
		cp.setLayout(new FitLayout());
		cp.add(grid);
		cp.setBottomComponent(grid.getToolBar());
		layout.add(cp, centerData);
	}

	private void initTree() {
		Map<String, Object> map = new HashMap<String, Object>();
		map.put(ExpandTree.TEACHER_ADD, false);
		map.put(ExpandTree.CHECKABLE, true);
		treePanel = new ExpandTree(Student.class,map) {
			@Override
			protected void load(Object loadConfig, Map<String, Object> configs,
					AsyncCallback<List<TreeNode>> callback) {

				service.getChildren((TreeNode) loadConfig, configs, callback);
			}

			@Override
			protected void load(String[] items) {

				Config c = new Config();
				c.put(ExpandTree.GRADE_ITEMS, items);
				treePanel.load(Student.class,c);
			}
		};
		layout.add(treePanel, westData);
	}

	private void initListeners() {
		treePanel.tree.addListener(Events.OnDoubleClick,
				new Listener<TreePanelEvent<BeanModel>>() {

					@Override
					public void handleEvent(TreePanelEvent<BeanModel> be) {

						BeanModel bm = be.getItem();
						TreeNode treeNode = bm.getBean();
						Config c = new Config();
						c.put("treeNode", treeNode);
						boolean b = grid.hasConfig(ExpandTree.TREE_NODE,
								treeNode);
						if (!b)
							grid.load(c);
					}

				});
	}

	private void initTool() {
		/**
		 * 创建工具栏
		 */

		ToolBar tb = new ToolBar();
		Label l1 = new Label("索引属性：");
		Label l2 = new Label("索引数值：");
		final SimpleComboBox<String> combo = new SimpleComboBox<String>();
		final TextField<String> lastName = new TextField<String>();
	
		Button b3 = new Button("清空条件");
		b3.setIconAlign(IconAlign.TOP);
		b3.setScale(ButtonScale.LARGE);
		b3.setIcon(AbstractImagePrototype.create(Resources.ICONS.clear()));
		// b4.setIcon(AbstractImagePrototype.create(Resources.ICONS.find2()));
		b3.addSelectionListener(new SelectionListener<ButtonEvent>() {

			@Override
			public void componentSelected(ButtonEvent ce) {
				// TODO Auto-generated method stub
				grid.clearConifgs();
				combo.clear();
				lastName.clear();

			}
		});
		
		Button verify = new Button("信息审核");
		verify.setIconAlign(IconAlign.TOP);
		verify.setScale(ButtonScale.LARGE);
		verify.setIcon(AbstractImagePrototype.create(Resources.ICONS.shenhe()));
		verify.addSelectionListener(new SelectionListener<ButtonEvent>() {

			@Override
			public void componentSelected(ButtonEvent ce) {
				// TODO Auto-generated method stub
				Student s = grid.getSelectionModel().getSelectedItem()
						.getBean();
				if (s != null)
					examineAndVerify(s);
			}
		});

		combo.add(ByGrade.ADDRESS);
		combo.add(ByGrade.BEAN_JI);
		combo.add(ByGrade.BIRTHDAY);
		combo.add(ByGrade.BIRTHPLACE);
		combo.add(ByGrade.CELLPHONE);
		combo.add(ByGrade.DORMITORY);
		combo.add(ByGrade.NAME);
		combo.add(ByGrade.NATION);
		combo.add(ByGrade.POST_CODE);
		combo.add(ByGrade.QQ);
		combo.add(ByGrade.SEX);
		combo.add(ByGrade.SPECIALTY);
		combo.add(ByGrade.STUDENT_NUMBER);
		combo.add(ByGrade.TELLPHONE);
		combo.setForceSelection(true);
		combo.setTriggerAction(TriggerAction.ALL);
		final ButtonGroup group = new ButtonGroup(2);
		group.add(l1);
		group.add(combo);
		group.add(l2);
		group.add(lastName);
		Button b1 = new Button("模糊查询");
		b1.setIconAlign(IconAlign.TOP);
		b1.setScale(ButtonScale.LARGE);
		b1.setIcon(AbstractImagePrototype.create(Resources.ICONS.mohusearch()));
		b1.addSelectionListener(new SelectionListener<ButtonEvent>() {

			@Override
			public void componentSelected(ButtonEvent ce) {
				if (!combo.getValue().getValue().equals("")
						&& !lastName.getValue().equals("")) {
					Config c = new Config();
					GridConfig gc = new GridConfig();

					gc.put(combo.getValue().getValue(), lastName.getValue());
					c.put(ByGrade.GRID_FILTER, gc);
					grid.load(c);
				}
			}
		});

		tb.add(verify);
		tb.add(new SeparatorToolItem());
		tb.add(group);
		tb.add(new SeparatorToolItem());
		tb.add(b1);
		tb.add(new SeparatorToolItem());
		tb.add(b3);
		Button b4 = new Button("高级查询");
		b4.setIconAlign(IconAlign.TOP);
		b4.setScale(ButtonScale.LARGE);
		b4.setIcon(AbstractImagePrototype.create(Resources.ICONS.details()));
		b4.addSelectionListener(new SelectionListener<ButtonEvent>() {

			@Override
			public void componentSelected(ButtonEvent ce) {
				// TODO Auto-generated method stub
				hightSearch();
			}
		});
		tb.add(new SeparatorToolItem());
		tb.add(b4);
		cp.setTopComponent(tb);
	}
	public void hightSearch(){
		new QueryWindow(){

			@Override
			public void serverCommit(Map<String, Map<String, String>> map) {
				Map<String,Object> query = new HashMap<String,Object>();
				query.put(ByGrade.QUERY, map);
				grid.load(query);
			}
			
		};
	}
	public void windowFilter(Student student) {
		final Window window = new Window();
		window.setSize(1200, 500);
		window.setPlain(true);
		window.setModal(true);
		window.setBlinkModal(true);
		window.setHeading("详细信息");
		window.setLayout(new FitLayout());
		EntranceRegistrationPanel ent = new EntranceRegistrationPanel(student,
				false);
		StudentInformationPanel si = new StudentInformationPanel(student, false);
		ent.createForm();
		TabPanel panel = new TabPanel();
		panel.setBorders(false);
		TabItem item1 = new TabItem("入学信息");
		item1.setLayout(new FitLayout());
		item1.add(ent);

		TabItem item2 = new TabItem("基本信息");
		item2.setLayout(new FitLayout());
		item2.add(si);
		panel.add(item1);
		panel.add(item2);

		window.add(panel, new FitData(4));
		window.addButton(new Button("Close",
				new SelectionListener<ButtonEvent>() {
					@SuppressWarnings("deprecation")
					@Override
					public void componentSelected(ButtonEvent ce) {
						window.close();
					}
				}));

		window.setFocusWidget(window.getButtonBar().getItem(0));
		window.show();
	}

	public void examineAndVerify(Student student) {
		final Window window = new Window();
		window.setSize(1200, 500);
		window.setPlain(true);
		window.setModal(true);
		window.setBlinkModal(true);
		window.setHeading("审核");
		window.setLayout(new FitLayout());

		ToolBar toolbar = new ToolBar();
		final StudentInformationPanel si = new StudentInformationPanel(student,
				false);

		// 审核通过
		Button passButton = new Button("审核通过");
		passButton.setIconAlign(IconAlign.TOP);
		passButton.setScale(ButtonScale.LARGE);
		passButton
				.setIcon(AbstractImagePrototype.create(Resources.ICONS.pass()));
		passButton.addSelectionListener(new SelectionListener<ButtonEvent>() {

			@SuppressWarnings({ "unchecked", "rawtypes" })
			@Override
			public void componentSelected(ButtonEvent ce) {
				// TODO Auto-generated method stub
				TabItem tabItem = si.getTabPanel().getSelectedItem();
				BeanGridPanel beanGridPanel = (BeanGridPanel) tabItem.getItemByItemId(tabItem.getText());
				// BeanModel model =
				// (BeanModel)(beanGridPanel.getGrid().getSelectionModel().getSelectedItem());
				List<BeanModel> list = beanGridPanel.getGrid().getSelectionModel().getSelectedItems();
				pass(list, beanGridPanel.getGrid().getService());
				beanGridPanel.getGrid().getView().refresh(false);
			}
		});
		toolbar.add(passButton);

		// 审核不通过
		Button notPass = new Button("审核不通过");
		notPass.setIconAlign(IconAlign.TOP);
		notPass.setScale(ButtonScale.LARGE);
		notPass.setIcon(AbstractImagePrototype.create(Resources.ICONS.notPass()));
		notPass.addSelectionListener(new SelectionListener<ButtonEvent>() {

			@SuppressWarnings({ "unchecked", "rawtypes" })
			@Override
			public void componentSelected(ButtonEvent ce) {
				// TODO Auto-generated method stub
				TabItem tabItem = si.getTabPanel().getSelectedItem();
				BeanGridPanel beanGridPanel = (BeanGridPanel) tabItem
						.getItemByItemId(tabItem.getText());
				List<BeanModel> list = beanGridPanel.getGrid()
						.getSelectionModel().getSelectedItems();
				notPass(list, beanGridPanel.getGrid().getService());
				beanGridPanel.getGrid().getView().refresh(false);
			}
		});
		toolbar.add(notPass);

		window.setTopComponent(toolbar);

		window.add(si);
		window.show();
	}

	/**
	 * 审核通过
	 * 
	 * @param selected
	 * @param service
	 */
	public void pass(List<BeanModel> selected, BaseServiceAsync service) {
		List<PO> list = new ArrayList<PO>();
		for (BeanModel model : selected) {
			StateProperty state = model.getBean();
			state.setState("yes");
			PO po = (PO) state;
			list.add(po);
		}
		service.update(list, new AsyncCallback<Void>() {

			@Override
			public void onFailure(Throwable caught) {
				// TODO Auto-generated method stub
				Info.display("提示", "审核失败！！！");
			}

			@Override
			public void onSuccess(Void result) {
				// TODO Auto-generated method stub
				Info.display("提示", "审核成功！！！");
			}
		});
	}

	/**
	 * 审核不通过
	 * 
	 * @param selected
	 * @param service
	 */
	public void notPass(List<BeanModel> selected, BaseServiceAsync service) {
		List<PO> list = new ArrayList<PO>();
		for (BeanModel model : selected) {
			StateProperty state = model.getBean();
			state.setState("no");
			PO po = (PO) state;
			list.add(po);
		}
		service.update(list, new AsyncCallback<Void>() {

			@Override
			public void onFailure(Throwable caught) {
				// TODO Auto-generated method stub
				Info.display("提示", "审核失败！！！");
			}

			@Override
			public void onSuccess(Void result) {
				// TODO Auto-generated method stub
				Info.display("提示", "审核成功！！！");
			}
		});
	}
}
